package com.eos.orm.c3p0.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.sql.*;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.SysUser;
import com.hf.utils.StringUtil;

/**
 * sys_user系统用户表的分页查询及相关批处理操作
 * 
 * @author sz
 * 
 */
public class SysUserDaoImpl extends AbstractC3p0Dao<SysUser> {

	/**
	 * 查询的条件：id/user_id/user_email/role_id/deal_flag/status
	 */
	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString();
		if (!StringUtil.isEmpty(map.get("id")))
			condition.append("id='").append(map.get("id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("user_id")))
			condition.append("user_id='").append(
					map.get("user_id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("user_email")))
			condition.append("user_email='").append(
					map.get("user_email")).append("' and ");
		if (!StringUtil.isEmpty(map.get("role_id")))
			condition.append("role_id='").append(map.get("role_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("deal_flag")))
			condition.append("deal_flag='").append(map.get("deal_flag"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("status")))
			condition.append("status=").append(map.get("status")).append(
					" and ");
		return condition.append("1=1").toString();
	}

	@Override
	public List<SysUser> findPage(Map<String, String> map) {
		List<SysUser> templist = new ArrayList<SysUser>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				SysUser temp = new SysUser();
				temp.setId(rs.getInt("id"));
				temp.setUserId(rs.getString("user_id"));
				temp.setUserEmail(rs.getString("user_email"));
				temp.setUserName(rs.getString("user_name"));
				temp.setRoleId(rs.getInt("role_id"));
				temp.setUserTelephone(rs.getString("user_telephone"));
				temp.setUserMobile(rs.getString("user_mobile"));
				temp.setUserDesc(rs.getString("user_desc"));
				temp.setInsertTime(rs.getString("insert_time"));
				temp.setModifyTime(rs.getString("modify_time"));
				temp.setStatus(rs.getString("status"));
				temp.setDealFlag(rs.getString("deal_flag"));
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("SysUserDaoImpl exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
}
